﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Text;
using Core.Common;

namespace TTSClient.Models
{
    public class TTSModel
    {
        /// <summary>
        /// 序号
        /// </summary>
        [Column("index")]
        public int Index { get; set; }
        /// <summary>
        /// 发音人代码
        /// </summary>
        [Column("code"), MaxLength(32)]
        public string Code { get; set; }
        /// <summary>
        /// 文本文字
        /// </summary>
        [Column("text"), MaxLength(1024)]
        public string Text { get; set; }
        /// <summary>
        /// 储存路径
        /// </summary>
        [Column("path"), MaxLength(256)]
        public string Path { get; set; }
        /// <summary>
        /// 发音人
        /// </summary>
        [NotMapped]
        public string VcnText { get; set; }
        /// <summary>
        /// 文件完整路径
        /// </summary>
        [NotMapped]
        public string FullPath { get; set; }
        /// <summary>
        /// 转换是否已经开始
        /// </summary>
        [Column("started")]
        public bool Started { get; set; }
        /// <summary>
        /// 转换是否已经开始
        /// </summary>
        [Column("ended")]
        public bool Ended { get; set; }
        /// <summary>
        /// 转换结果, 是否已完成
        /// </summary>
        [Column("success")]
        public bool Success { get; set; }
        /// <summary>
        /// 是否已写入文件
        /// </summary>
        [Column("written")]
        public bool Written { get; set; }
        public override string ToString()
        {
            return Index + LINE_SEPARATOR + Code + LINE_SEPARATOR + Path + LINE_SEPARATOR + Text;
        }

        private const string LINE_SEPARATOR = "#_#_#";
        private static readonly string DATA_FILE_NAME = System.IO.Path.Combine(AppSetting.Current.RootPath, "tts.dat");
        public static readonly ICollection<TTSModel> TTSModels = new List<TTSModel>();
        private static TTSModel ReadLine(string line)
        {
            if (line.IsNotNullOrEmpty() && line.Contains(LINE_SEPARATOR))
            {
                var chars = line.Split(LINE_SEPARATOR);
                if (chars.Length >= 4)
                {
                    return new TTSModel()
                    {
                        Index = Convert.ToInt32(chars[0]),
                        Code = chars[1],
                        Path = chars[2],
                        Text = chars[3]
                    };
                }
            }

            return null;
        }

        /// <summary>
        /// 从数据文件读
        /// </summary>
        public static void Read(ICollection<Language> languages)
        {
            if (File.Exists(DATA_FILE_NAME))
            {
                var contents = File.ReadAllLines(DATA_FILE_NAME);
                if (contents.IsNotNullOrEmpty())
                {
                    foreach (var line in contents)
                    {
                        var model = ReadLine(line);
                        if (model != null)
                        {
                            model.VcnText = languages.FirstOrDefault(x => x.Code == model.Code)?.Text;
                            model.Success = true;
                            model.Written = true;
                            TTSModels.Add(model);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 写入数据文件
        /// </summary>
        public static void Write()
        {
            if (TTSModels.IsNotNullOrEmpty())
            {
                var contents = new List<string>();
                foreach (var model in TTSModels.Where(x => !x.Written && x.Success))
                {
                    contents.Add(model.ToString());
                }

                if (File.Exists(DATA_FILE_NAME))
                {
                    File.WriteAllLines(DATA_FILE_NAME, contents, Encoding.UTF8);
                }
                else
                {
                    File.AppendAllLines(DATA_FILE_NAME, contents, Encoding.UTF8);
                }
            }
        }
    }
}